package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"tuowei.com/app/api/models"
	"tuowei.com/app/api/service/dto"
	cDto "tuowei.com/common/dto"
	"tuowei.com/pkg/xlog"
)

type ApiLearningList struct {
	service.Service
}

func (e *ApiLearningList) GetPages(c *dto.GetLearningListReq, list *[]models.LearningList, count *int64) error {
	var model models.SysLearningList
	err := e.Orm.
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
		).Table(model.TableName()).
		Model(model).Where("buyer_id = ?", c.BuyerId).Order("id desc").Find(&list).Limit(-1).Offset(-1).Count(count).Error
	if err != nil {
		return err
	}
	return nil
}

//TODO 创建学习记录
func (e *ApiLearningList) Insert(c *dto.SysLearningListInsertReq, data *models.SysLearningList) error {
	var model models.SysLearningList
	c.Generate(data)
	err := e.Orm.Model(model).Create(&data).Error
	if err != nil {
		return err
	}
	return nil
}

// Get 获取SysCourse对象
func (e *ApiLearningList) Get(studentId int, data *[]models.LearningList) error {
	var model models.SysLearningList

	err := e.Orm.Model(&model).
		Where("buyer_id = ?", studentId).
		Find(&data).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil
		} else {
			xlog.Error(err)
			return err
		}
	}

	return nil
}

//func (e *ApiLearningList) LearningStudy(req *dto.LearnListVideoStudyReq) error {
//
//}
